
function buildTable(table_id, items, columns, sort, asc) {
	var old_table = document.getElementById(table_id);
	var new_table = old_table.cloneNode(true);    // Modify outside DOM.
	var column_width = window.innerWidth / columns.length;

	while (new_table.hasChildNodes()) new_table.removeChild(new_table.lastChild);
	
	// Build header row.
	var thead = document.createElement('thead');
	var thead_tr = document.createElement('tr');
	thead_tr.id = 'thead_tr';
	thead.appendChild(thead_tr);
	for (var c = 0; c < columns.length; c++) {
		var th = document.createElement('th');
		//th.width = column_width;
		th.appendChild(document.createTextNode(columns[c]));
		if(columns[c] === sort){
			th.className = 'sorting noSelect ' + (asc ? 'sorting_asc' : 'sorting_desc');
		}
		else{
			th.className = 'sorting noSelect';
		}
		thead_tr.appendChild(th);
	}
	var th = document.createElement('td');
	var menu_button = document.createElement('button');
	menu_button.id = 'table_menu_button';
	th.appendChild(menu_button);
	thead_tr.appendChild(th);
	new_table.appendChild(thead);

	var tbody = document.createElement('tbody');
	var tr = document.createElement('tr');
	//Add hidden row for correct allignment
	for (var c = 0; c < columns.length; c++) {
		var th = document.createElement('th');
			//td.width = column_width;
			var text = columns[c];
			th.appendChild(document.createTextNode(text));
			tr.appendChild(th);
	}
	//Add hidden button
	var th = document.createElement('th');
	th.appendChild(document.createTextNode(""));
	th.style.width = "22px"
	tr.appendChild(th);
	tbody.appendChild(tr);

	for (var r = 0; r < items.length; r++) {
		var tr = document.createElement('tr');
		if (r % 2 == 0) tr.className = 'odd';
		for (var c = 0; c < columns.length; c++) {
			var td = document.createElement('td');
			//td.width = column_width;
			var text = items[r][columns[c]];
			if (text === undefined) text = '';
			text += "";
			if(text.search(/local\/data\/upload/) != -1){
				console.log("löyty");
				var a = document.createElement('a');
				a.appendChild(document.createTextNode('Link'));
				a.href = text.replace('local','');
				a.title = text.split('/').pop();
				a.target = "_blank";
				td.appendChild(a);
			}else if(text.search(/http|https|ftp/) != -1){
				console.log("löyty");
				var a = document.createElement('a');
				a.appendChild(document.createTextNode('Link'));
				a.href = text;
				a.title = text;
				//a.target = "_blank";
				td.appendChild(a);
				$(a).click(function(e) {
					if (this.href.search(/\.png$/) == -1) return;
					console.log(this);
					e.preventDefault();
					$('#dialog_microscope img').attr('src', this.href);
					$('#dialog_microscope').dialog({
						width: 0.42 * $(window).width(),
						height: 0.45 * $(window).width()
					});
					$('#dialog_microscope').dialog('open');
				});
			}else{
				td.appendChild(document.createTextNode(text));

			}
			tr.appendChild(td);
		}
		var td = document.createElement('td');
		tr.appendChild(td);
		tbody.appendChild(tr);
	}
	new_table.appendChild(tbody);
	old_table.parentNode.replaceChild(new_table, old_table);  // Insert to DOM.
	resetHeaderWidth();
	$("#table_menu_button").button({
		icons: {primary: 'column_button_icon'},
		text: false
	});

}

function resetHeaderWidth(){
	var ref_tds = $("#table > tbody > tr:first-child > th");
	var header_ths = $("thead > tr > th").each(function(idx, ele){
		$(ele).css('min-width',$(ref_tds[idx]).innerWidth()-28);
		$(ele).css('max-width',$(ref_tds[idx]).innerWidth()-28);
	});
}

function addItems(table_id, items, columns){
	var tbody = document.getElementById(table_id).getElementsByTagName('tbody')[0];

	for (var r = 0; r < items.length; r++) {
		var tr = document.createElement('tr');
		if (r % 2 == 0) tr.className = 'odd';
		for (var c = 0; c < columns.length; c++) {
			var td = document.createElement('td');
			var text = items[r][columns[c]];
			if (text == undefined) text = '';
			td.appendChild(document.createTextNode(text));
			tr.appendChild(td);
		}
		tbody.appendChild(tr);
	}

	resetHeaderWidth();
}
